package com.jq.aution.component;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 动态定时类
 * Created by jiangqi on 2020/10/16
 */
//@Lazy(value=false)
//@Component
//@EnableScheduling
@Controller
@Api(tags = "DynamicCronTask", description = "定时任务管理")
public class DynamicCronTask{
    //public static String cron = "0/10 * * * * ?";
/*
    @ApiOperation("设置定时")
    @RequestMapping(value = "/setDynamicCronTask",method = RequestMethod.POST)
    @ResponseBody
    public String setSpringDynamicCronTask(@RequestParam String setCron) {
        System.out.println("cron setDynamicCronTaskto:" + setCron);
        taskRegistrar.addTriggerTask(addTriggerTask(setCron));
        return "";
    }


    public TriggerTask addTriggerTask(String setCron){
        return new TriggerTask(new Runnable() {
            @Override
            public void run() {
                // 逻辑任务
                System.out.println("dynamicCronTask is running...");
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 任务触发，可修改任务的执行周期
                CronTrigger trigger = new CronTrigger(setCron);
                System.out.println("cron:" + setCron);
                Date nextExecutor = trigger.nextExecutionTime(triggerContext);
                return nextExecutor;
            }
        });
    }*/
   /* @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegister) {
        taskRegister.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                // 逻辑任务
                System.out.println("dynamicCronTask is running...");
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 任务触发，可修改任务的执行周期
                CronTrigger trigger = new CronTrigger(cron);
                System.out.println("cron:" + cron);
                Date nextExecutor = trigger.nextExecutionTime(triggerContext);
                return nextExecutor;
            }
        });
    }*/
}
